<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.0&type=webgl&ak=mQnIhkcrCDE5ZVY0xIhpqi6w8BEINcx4"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/library/Heatmap/2.0/src/Heatmap_min.js"></script>
    <title>热力图</title>
    <style type="text/css">
		ul,li{list-style: none;margin:0;padding:0;float:left;}
		html{height:100%}
		body{height:100%;margin:0px;padding:0px;font-family:"微软雅黑";}
		#container{height:85%;width:100%;}
		#r-result{width:100%;margin-left:20px}
		#searchbar{margin-left:20px}
    </style>
</head>
<body>
	<div id="container"></div>
	<div id="searchbar" class="search map_search">
		<input type="text" class="text" id="localvalue" value="请输入关键字进行搜索" οnfοcus="foucs_(this,'请输入关键字进行搜索','')" οnblur="blur_(this,'请输入关键字进行搜索','')" callback="beginsearch(l_local)"/>
		<input class="button" type="button" value="百度一下" οnclick='localsearch();' />
	</div>
	<div id="r-result">

		输入省、直辖市或县名称：<input type="text" id="districtName" style="width:160px" value="北京市海淀区">
		<input type="button" οnclick="getBoundary()" value="获取轮廓线">
		<input type="button" οnclick="Clear()" value="清除轮廓线">
		</br>
		<input type="button"  οnclick="f();" value="显示热力图"/><input type="button"  οnclick="closeHeatmap();" value="关闭热力图"/>
		</br>
		<input type="range" name="rangeInput" min="10" max="100" step='10' οnchange="updateTextInput1(this.value);f()">
		<input type="text" id='a' style="width:60px" value='50' />
		<input type="button" value="更改最大值"  title='最大值越小视觉上热度越高'/>
		</br>
		<input type="range" name="rangeInput" min="0" max="50" οnchange="updateTextInput(this.value);f()">
		<input type="text" id="r" value="15" style="width:60px">
		<input type="button"  value="更改半径" title='半径越小热力图看起来越零散'/>
		</br>



	</div>
</body>
</html>
<script type="text/javascript">

	function updateTextInput(val) {
          document.getElementById('r').value=val;
        }

	function updateTextInput1(val) {
          document.getElementById('a').value=val;
        }

	function getBoundary(){
    var bdary = new BMap.Boundary();
    var name = document.getElementById("districtName").value;
    bdary.get(name, function(rs){       //获取行政区域
        //map.clearOverlays();        //清除地图覆盖物

    // document.getElementById('Div1').innerText = rs.boundaries;
        var count = rs.boundaries.length; //行政区域的点有多少个
        for(var i = 0; i < count; i++){
            var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 4, strokeColor: "#0000FF"}); //建立多边形覆盖物
            map.addOverlay(ply);  //添加覆盖物
            map.setViewport(ply.getPath());    //调整视野
        }
    });
}
	function Clear(){
	map.clearOverlays();
	}

    var map = new BMap.Map("container");          // 创建地图实例

	map.addControl(new BMap.NavigationControl());        // 添加平移缩放控件
	map.addControl(new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT}));             // 添加比例尺控件
	map.addControl(new BMap.OverviewMapControl());

	function localsearch() {
		map.clearOverlays();
		var local = new BMap.LocalSearch(map, {
		renderOptions:{map: map}
	});
	var place=document.getElementById("localvalue").value;
	local.search(place);

	}


    var point = new BMap.Point(116.418261, 39.921984);
    map.centerAndZoom(point, 12);             // 初始化地图，设置中心点坐标和地图级别
    map.enableScrollWheelZoom(); // 允许滚轮缩放

    var points =[//这里输入点的数据
    ];

    if(!isSupportCanvas()){
    	alert('热力图目前只支持有canvas支持的浏览器,您所使用的浏览器不能使用热力图功能~')
    }
	//详细的参数,可以查看heatmap.js的文档 https://github.com/pa7/heatmap.js/blob/master/README.md
	//参数说明如下:
	/* visible 热力图是否显示,默认为true
     * opacity 热力的透明度,1-100
     * radius 势力图的每个点的半径大小
     * gradient  {JSON} 热力图的渐变区间 . gradient如下所示
     *	{
			.2:'rgb(0, 255, 255)',
			.5:'rgb(0, 110, 255)',
			.8:'rgb(100, 0, 255)'
		}
		其中 key 表示插值的位置, 0~1.
		    value 为颜色值.
     */
	heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":20});
	map.addOverlay(heatmapOverlay);
	heatmapOverlay.setDataSet({data:points,max:700});
	//是否显示热力图

	function f(){
		var m1 = document.getElementById("a").value;
		var r1 = document.getElementById("r").value;
		heatmapOverlay.hide();
		heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":r1});
		map.addOverlay(heatmapOverlay);
		heatmapOverlay.setDataSet({data:points,max:m1});
		heatmapOverlay.show();
	}



    function openHeatmap(){
        heatmapOverlay.show();
    }
	function closeHeatmap(){
        heatmapOverlay.hide();
    }
	closeHeatmap();
    function setGradient(){
     	/*格式如下所示:
		{
	  		0:'rgb(102, 255, 0)',
	 	 	.5:'rgb(255, 170, 0)',
		  	1:'rgb(255, 0, 0)'
		}*/
     	var gradient = {};
     	var colors = document.querySelectorAll("input[type='color']");
     	colors = [].slice.call(colors,0);
     	colors.forEach(function(ele){
			gradient[ele.getAttribute("data-key")] = ele.value;
     	});
        heatmapOverlay.setOptions({"gradient":gradient});
    }
	//判断浏览区是否支持canvas
    function isSupportCanvas(){
        var elem = document.createElement('canvas');
        return !!(elem.getContext && elem.getContext('2d'));
    }
</script>